package com.doucat.support.component.recycle.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.doucat.support.component.recycle.holder.BaseRecyclerHolder;
import com.doucat.support.component.recycle.holder.FooterHolder;
import com.doucat.support.component.recycle.holder.HeaderHolder;
import com.doucat.support.component.recycle.holder.ItemRecyclerHolder;
import com.tonly.doucatlibrary.R;

import java.util.List;

/**
 * -----------------------------------------------------------------
 * Copyright (C) 2014-2016, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * File: BaseRecyclerAdapter.java
 * Author: win7
 * Version: V100R001C01
 * Create: 2021/1/26 11:04
 * Changes (from 2021/1/26)
 * -----------------------------------------------------------------
 * 2021/1/26 : Create BaseRecyclerAdapter.java (win7);
 * -----------------------------------------------------------------
 */
public abstract class FooterRecyclerAdapter<T> extends CommonRecyclerAdapter<T> {
    //脚布局
    public static final int TYPE_HEADER = 2;
    public static final int TYPE_FOOTER = 3;

    //当前刷新状态，默认为隐藏
    private int refreshSate=HIDE_REFRESH;
    //当前加载状态，默认为加载完成
    private int loadState=HIDE_LOADING;
    //隐藏加载更多
    public static final int HIDE_REFRESH =0;
    //隐藏加载正在刷新
    public static final int HIDE_LOADING =1;
    //正在加载
    public static final int LOADING=2;
    //加载完成
    public static final int LOADING_COMPLETE=3;
    //加载到底
    public static final int LOADING_END=4;

    public static final int REFRESHING=5;
    public static final int REFRESHING_COMPLETE=6;
    public static final int REFRESHING_END=7;

    public FooterRecyclerAdapter(Context context) {
        super(context);
    }

    public FooterRecyclerAdapter(Context context, List data) {
        super(context, data);
    }

    @Override
    public BaseRecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseRecyclerHolder holder = null;
        View view;
        switch (viewType){
            case TYPE_HEADER:
                view = LayoutInflater.from(context).inflate(R.layout.layout_recycle_footer,parent,false);
                holder = new HeaderHolder(view);
                break;
            case TYPE_FOOTER:
                view = LayoutInflater.from(context).inflate(R.layout.layout_recycle_header,parent,false);
                holder = new FooterHolder(view);
                break;
            case TYPE_ITEM:
                view = LayoutInflater.from(context).inflate(getLayoutRes(),parent,false);
                holder = new ItemRecyclerHolder(view);
                break;
            default:
//                throw new IllegalStateException("Unexpected value: " + viewType);
                return  super.onCreateViewHolder(parent,viewType);

        }
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseRecyclerHolder holder, int position) {
        /**
         * 保护真正的list不会数组越界
         * 头部将真正的list往下挤了一格，所以真正的list。position要-1
         */
//        int newPosition=position-1;
        int newPosition=position;
        if(holder instanceof ItemRecyclerHolder){
            holder.itemView.setFocusable(true);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListener!=null){
                        onItemClickListener.onItemClick(null,holder.itemView,position,holder.itemView.getId());
                    }
                }
            });
            onBindItemViewHolder(holder,newPosition);
        }else if(holder instanceof FooterHolder){
            holder.itemView.setEnabled(false);
            TextView tvLoad= (TextView) holder.getView(R.id.tvLoad);
            switch (loadState){
                case HIDE_LOADING:
                    holder.itemView.setVisibility(View.INVISIBLE);
                    break;
                case LOADING:
                    holder.itemView.setVisibility(View.VISIBLE);
                    tvLoad.setText(context.getString(R.string.loading));
                    break;
                case LOADING_COMPLETE:
                    holder.itemView.setVisibility(View.VISIBLE);
                    tvLoad.setText(context.getString(R.string.loading_complete));
                    break;
                case LOADING_END:
                    holder.itemView.setVisibility(View.VISIBLE);
                    tvLoad.setText(context.getString(R.string.loading_end));
                    break;
            }
        }else if(holder instanceof HeaderHolder){
            holder.itemView.setEnabled(false);
            TextView tvLoad= (TextView) holder.getView(R.id.tvLoad);
            switch (refreshSate){
                case HIDE_REFRESH:
                    holder.itemView.setVisibility(View.GONE);
                    break;
                case REFRESHING:
                    holder.itemView.setVisibility(View.VISIBLE);
                    tvLoad.setText(context.getString(R.string.loading));
                    break;
                case REFRESHING_COMPLETE:
                    holder.itemView.setVisibility(View.VISIBLE);
                    tvLoad.setText(context.getString(R.string.loading_complete));
                    break;
                case REFRESHING_END:
                    holder.itemView.setVisibility(View.VISIBLE);
                    tvLoad.setText(context.getString(R.string.loading_end));
                    break;
            }
        }
    }
    protected abstract void onBindItemViewHolder(BaseRecyclerHolder holder, int position);
    @Override
    public int getItemViewType(int position) {
        //position为最后一项目时，是底部
        if (position == getItemCount()-1) {
            Log.v(TAG,position+" TYPE_FOOTER");
            return TYPE_FOOTER;
        } else {
            Log.v(TAG,position+" TYPE_ITEM");
            return TYPE_ITEM;
        }
    }

    @Override
    public int getItemCount() {
        //增加了项加载更多
//        return items.size()+2;
        return items.size()+1;
    }

    //设置上拉的时候改变对应的状态
    public void setLoadState(int loadState){
        this.loadState=loadState;
        notifyDataSetChanged();
    }

    //设置上拉的时候改变对应的状态
    public void setRefreshSate(int loadState){
        this.refreshSate=loadState;
        notifyDataSetChanged();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager=recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager){
            final GridLayoutManager gridLayoutManager= (GridLayoutManager) manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    //如果是脚布局据合并单元格
                    return (getItemViewType(position)==TYPE_FOOTER)?1:gridLayoutManager.getSpanCount();
                }
            });
        }
    }
}
